package com.example.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.StudentClassesTuitionDao;
import com.example.domain.StudentClassesTuition;
import com.example.result.Result;
import com.example.result.ResultFactory;
import com.example.service.StudentClassesTuitionService;
import com.example.tool.DateTool;

@Service
public class StudentClassesTuitionServiceImpl implements StudentClassesTuitionService {

    @Autowired
    StudentClassesTuitionDao studentClassesTuitionDao;

    /**
     * @describe 添加学生班级缴费信息
     */
    @Override
    public Result addStudentClassesTuition(StudentClassesTuition studentClassesTuition) {
        if (studentClassesTuition != null) {
            // 获取系统当前时间作为记录创建时间
            studentClassesTuition.setRecordTime(DateTool.getTimestamp());
        }
        // 调用JPA保存实体方法
        StudentClassesTuition studentClassesTuition1 = studentClassesTuitionDao.save(studentClassesTuition);

        if (studentClassesTuition1 != null) {
            return ResultFactory.buildSuccessResult(studentClassesTuition1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 更新学生班级缴费信息
     */
    @Override
    public Result updateStudentClassesTuition(StudentClassesTuition studentClassesTuition) {
        if (studentClassesTuition != null) {
            // 获取系统当前时间作为记录更新时间
            studentClassesTuition.setUpdateTime(DateTool.getTimestamp());
        }
        // 调用JPA保存并刷新实体方法
        StudentClassesTuition studentClassesTuition1 = studentClassesTuitionDao.saveAndFlush(studentClassesTuition);

        if (studentClassesTuition1 != null) {
            return ResultFactory.buildSuccessResult(studentClassesTuition1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学生班级缴费编号删除学生班级缴费信息
     */
    @Override
    public Result deleteStudentClassesTuitionById(String studentTuitionId) {
        int row = studentClassesTuitionDao.deleteByStudentTuitionId(Integer.parseInt(studentTuitionId));

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 查询所有学生班级缴费信息
     */
    @Override
    public Result queryAllStudentClassesTuition() {
        List<StudentClassesTuition> list = studentClassesTuitionDao.findAll();
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据编号查询学生班级缴费信息
     */
    @Override
    public Result queryStudentClassesTuitionById(String studentTuitionId) {
        List<StudentClassesTuition> list = studentClassesTuitionDao.findByStudentTuitionId(Integer.parseInt(studentTuitionId));
        if (list != null) {
            return ResultFactory.buildSuccessResult(list);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 通过Excel批量添加学生班级缴费信息
     */
    @Override
    public Result addStudentClassesTuitionByExcel(List<StudentClassesTuition> list) {
        List<StudentClassesTuition> list1 = null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setRecordTime(DateTool.getTimestamp());
            }
            list1 = studentClassesTuitionDao.saveAll(list);
        }
        if (list1 != null) {
            return ResultFactory.buildSuccessResult(list1);
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 根据学生班级缴费编号批量删除学生班级缴费信息
     */
    @Override
    public Result deleteStudentClassesTuitionByIds(Map<String, String> ids) {
        List<Integer> listId = new ArrayList<Integer>();
        int row = 0;
        if (ids != null) {
            String idstr = ids.get("Ids");
            String[] id = idstr.split(",");

            for (int i = 0; i < id.length; i++) {
                listId.add(Integer.parseInt(id[i]));

            }
        }
        System.out.println(listId);
        if (listId != null) {
            row = studentClassesTuitionDao.deleteByStudentTuitionIdIn(listId);
        }

        if (row > 0) {
            return ResultFactory.buildDefaultSuccessResult();
        } else {
            return ResultFactory.buildDefaultFailResult();
        }
    }

    /**
     * @describe 学生班级缴费信息模糊查询
     */
    @Override
    public Result serachByInfo(String[] info) {
        return null;
        /*
         * List<Map<String, Object>> list = null;
         *
         *
         * if (list != null) { return ResultFactory.buildSuccessResult(list); } else {
         * return ResultFactory.buildDefaultFailResult(); }
         */
    }

}
